// 工具函数
import config from './config'
import Toast from '../static/Vant/toast/toast'
// HTTP Get请求函数
export function get(url, data) {
  return request(url, 'GET', data)
}
export function post(url, data) {
  return request(url, 'POST', data)

}

function request(url, method, data) {
  return new Promise((resolve, reject) => {
    wx.request({
      data,
      method,
      url,
      // url: config.host + url, //开发者服务器接口地址",
      success: res => {
        if (res.data.code === 0) {
          resolve(res.data.data)
        } else {
          resolve(res.data)
        }
      }
    });
  })
}
export function showSuccess(text) {
  wx.showToast({
    title: text, //提示的内容,
    icon: 'success', //图标,
  });
}
export function showToast(text) {
  Toast({
    message: text,
  });
}
export function showTimerToast(text, time) {
  Toast({
    message: text,
    duration: parseInt(time)
  });
}

// 生成流水单
export function randomNumber() {
  var now = new Date()
  var month = now.getMonth() + 1
  var day = now.getDate()
  var hour = now.getHours()
  var minutes = now.getMinutes()
  var seconds = now.getSeconds()
  // month = now.setMonth(month)
  month = month.toString().padStart(2, '0')
  day = day.toString().padStart(2, '0')
  // hour = now.setHours(hour)
  hour = hour.toString().padStart(2, '0')

  // minutes = now.setMinutes(minutes)
  minutes = minutes.toString().padStart(2, '0')
  // seconds = now.setSeconds(seconds)
  seconds = seconds.toString().padStart(2, '0')
  var time = now.getFullYear().toString() + month + day + hour + minutes + seconds
  return (time + (Math.floor((Math.random() * 1000000)).toString()).padStart(6, '9'))
}

// 生成随机字符串
export function randomChar() {
  let str = 'abcdefghijklmnopqrstuvwxyz0123456789'
  let arr = []
  for (let i = 0; i < 20; i++) {
    var res = str.substr(Math.floor(Math.random() * 0x10), 1)
    // var res = Math.ceil(Math.random() * (upper - lower)) + lower;
    // arr.push(String.fromCharCode(res))
    arr.push(res)
  }
  return arr.join('')
}


// 发送模板
function muban() {
  let {
    formId,
    openId
  } = wx.getStorageSync("userInfo");
  let param = {
    formId,
    openId,
    template_id: "_QV_0LMxX78v1zgivzjBJ-ix2DLjGgedsYhYs1BItTk",
    content: "يېڭى كىنو كەلدى",
    msg: "ئەڭ يېڭى كىنولارنى كۆرمەكچى بولسىڭىز قولايغا كېلىڭ"
  };
  let opts = {
    touser: param.openId,
    template_id: param.template_id,
    form_id: param.formId,
    data: {
      keyword1: {
        value: param.content,
        color: "#1d1d1d"
      },
      keyword2: {
        value: param.msg,
        color: "#ff0000"
      }
    },
    page: "/pages/AD/main?from=template"
  };
  this.$http
    .get(
      `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${"wx3ba6230358b8558a"}&secret=${"14a3d86df5bcdc269b2a8325cb353475"}`
    )
    .then(res => {
      console.log(res.data.access_token);
      param.access_token = res.data.access_token;
      wx.request({
        url: "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" +
          param.access_token, //开发者服务器接口地址",
        data: opts, //请求的参数",
        method: "POST",
        dataType: "json", //如果设为json，会尝试对返回的数据做一次 JSON.parse
        success: res => {},
        fail: () => {},
        complete: () => {}
      });
    });
}
